<template>
    <div class="page-content">
        <div class="comm-content" ref="box"></div>
    </div>
</template>
<script>
export default {
    data() {
        return {
            myEcharts: null
        }
    },
    computed:{
        themeStatus(){
            return this.$store.state.theme
        }
    },
    watch:{
        themeStatus(){
            this.myEcharts.dispose()
            this.initEcharts()
            this.getdata()
            this.screen()
        }
    },
    mounted() {
        this.initEcharts()
        this.getData()
        window.addEventListener('resize',this.screen)
        this.screen()
    },
    destroyed(){
        window.removeEventListener('reset',this.screen)
    },
    methods: {
        // 适配屏幕
        screen(){
            this.myEcharts.resize()
            // 计算一个文字大小的比例
            let width =  this.$refs.box.offsetWidth
            let num = width / 100*5
            let options={
                title:{
                    textStyle:{ //设置主标题样式
                        fontSize:num
                    }
                }
            }
            // 修改图标样式以后要重绘
            this.myEcharts.setOption(options)
        },
        // 初始化图标
        initEcharts() {
            let box = this.$refs.box
            // 初始化echarts
            this.myEcharts = this.$echarts.init(box,this.themeStatus)
            let options = { //设置图标的属性和样式
                title: { //主标题
                    text: '各地区种植面积统计TOP10',
                    left: 10,
                    top: 10
                },
                grid: {
                    left: '10%',
                    right: '10%',
                    top: '10%',
                    bottom: '10%'
                },
                tooltip: {
                    show: true
                },
                xAxis: {
                    show: true,
                    type: 'value',
                    axisLine: {
                        show: true
                    }
                },
                toolbox: {  //工具箱
                    show: true,
                    feature: {
                        saveAsImage: { //保存图片
                            type: 'png'
                        },
                        dataView: {
                            show: true
                        }
                    }
                },
                axisPointer: {
                    show: true
                },
                yAxis: {
                    show: true,
                    type: 'category'
                },
                series: [{
                    type: 'bar',
                    label: {  //定义显示数据
                        show: true,
                        position: 'right'
                    },
                    itemStyle: { // 设置图标颜色
                        opactiy: 0.8, //柱状透明度
                        borderRadius:[0,15,15,0],
                        color:{
                            type:'linear',
                            x:0,
                            y:0,
                            x1:1,
                            y1:0,
                            colorStops:[
                                {
                                offset:0,
                                color:'#0F3357',
                            },
                            {
                                offset:1,
                                color:'#36BFE5',
                            }
                        ]
                        }
                    }
                }],


            }
            // 绘制图标
            this.myEcharts.setOption(options)
        },
        // 获取数据
        async getData() {
            let dataList = await this.$http('/api/plantingAreaTop')
            dataList.data.sort(function (a, b) {
                return a.area - b.area
            })
            let dataClass = dataList.data.map(item => {
                return item.name
            })
            let dataArea = dataList.data.map(item => {
                return item.area
            })
            this.setEcharts(dataClass, dataArea);
        },
        // 绘制图标
        setEcharts(dataClass, dataArea) {
            console.log(dataClass, dataArea);
            let options = {
                yAxis: {
                    data: dataClass
                },
                series: [{
                    // type: 'bar',
                    data: dataArea
                }]
            }
            this.myEcharts.setOption(options)
        }
    }


}
</script>
<style ></style>